package com.waa.gulimall.order.listener;


import com.rabbitmq.client.Channel;
import com.waa.gulimall.common.to.mq.SeckilOrderTo;
import com.waa.gulimall.order.entity.OrderEntity;
import com.waa.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "order.seckill.order.queue")
@Component
@Slf4j
public class OrderSeckillListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(SeckilOrderTo seckilOrderTo, Channel channel, Message message) throws IOException {

        try {
            log.info("准备创建秒杀单的详细信息。。。");
            orderService.createSeckillOrder(seckilOrderTo);
            // 关闭订单成功后，ack信息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }


}
